<template>
  <el-dialog :visible.sync="visible" title="#入库计划录入" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="砂芯图号" prop="spuNo">
            <el-select v-model="dataForm.spuNo" filterable placeholder="请选择砂芯" @change="pickSpuNo">
              <el-option v-for="item in options" :key="item.spuId" :label="item.spuNo" :value="item">
                <span style="float: left">{{ item.spuNo }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.lotNo }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="需求数量" prop="amount">
            <el-input-number v-model="dataForm.amount" :min="2" :max="24"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="所属站台" prop="stationNo">
        <el-radio-group v-model="dataForm.stationNo" size="small">
          <el-radio border label="1F_UP_ST11">6号机械臂</el-radio>
          <el-radio border label="1F_UP_ST12">5号机械臂</el-radio>
          <el-radio border label="1F_UP_ST13">4号机械臂</el-radio>
          <el-radio border label="1F_UP_ST14">3号机械臂</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="comments">
        <el-input type="textarea" v-model="dataForm.comments"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        purchaseId: '',
        purchaseType: '入库上架',
        spuNo: '',
        lotNo:'',
        amount: '1',
        stationNo: '',
        comments: ''
      },
      options: []
    }
  },
  computed: {
    dataRule() {
      return {
        spuNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        amount: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        stationNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      //重置一下计划信息
      this.dataForm = this.$options.data().dataForm;
      this.getSpuInfo();
    },
    // 获取砂芯图号
    getSpuInfo() {
      this.$http.get(`/wms/spu-info/list`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.options = res.data;
      }).catch(() => { })
    },
    pickSpuNo(value) {
      this.dataForm.spuNo=value.spuNo;
      this.dataForm.lotNo=value.lotNo;
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.purchaseId ? 'post' : 'put']('/wms/purchase/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
